Field.resetDirty   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 4
dl 0
loc 4
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like Field.resetDirty often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import {ModelInterface, TableInterface} from "../../JeloquentInterfaces";
2
3
export default class Field {
4
5
    protected $fieldValue: unknown;
6
7
    protected $name: string;
8
9
    protected $originalValue: unknown;
10
11
    protected $parent: ModelInterface;
12
13
    protected $previousValue: unknown;
14
15
    private _isPrimary: boolean;
16
17
    /**
18
     *
19
     * @param name
20
     * @param isPrimary
21
     */
22
    constructor(name: string, isPrimary = false) {
23
        this._isPrimary = isPrimary;
24
        this.$fieldValue = null;
25
        this.$previousValue = undefined;
26
        this.$originalValue = undefined;
27
        this.$parent = null;
28
        this.$name = name;
29
    }
30
31
    get isDirty(): boolean {
32
        return this.$fieldValue != this.$previousValue;
33
    }
34
35
    get isPrimary(): boolean {
36
        return this._isPrimary;
37
    }
38
39
    get name(): string {
40
        return this.$name;
41
    }
42
43
    get originalValue(): unknown {
44
        return this.$originalValue;
45
    }
46
47
    get previousValue(): unknown {
48
        return this.$previousValue;
49
    }
50
51
    get value(): unknown {
52
        return this.$fieldValue;
53
    }
54
55
    set _value(newValue: unknown) {
56
        if (this.$originalValue === undefined) {
57
            this.$originalValue = JSON.parse(JSON.stringify(this.value ?? newValue));
58
        }
59
        this.$previousValue = JSON.parse(JSON.stringify(this.value));
60
        this.$fieldValue = newValue;
61
    }
62
63
64
    // eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
65
    set value(newValue: unknown) {
66
        if (this.$previousValue === undefined) {
67
            this.$previousValue = JSON.parse(JSON.stringify(this.value ?? newValue));
68
        }
69
70
        if (this.$originalValue === undefined) {
71
            this.$originalValue = JSON.parse(JSON.stringify(this.value ?? newValue));
72
        }
73
74
        this.$previousValue = JSON.parse(JSON.stringify(this.value));
75
76
        this.$fieldValue = newValue;
77
    }
78
79
    resetDirty(): void {
80
        this.$originalValue = JSON.parse(JSON.stringify(this.$fieldValue));
81
        this.$previousValue = JSON.parse(JSON.stringify(this.$fieldValue));
82
    }
83
84
    setName(): Field {
85
        return this;
86
    }
87
88
    setup(parent: ModelInterface): Field {
89
        this.$parent = parent;
90
        return this.setName().setParentProperties();
91
    }
92
93
    tableSetup(table: TableInterface): void {
94
        console.info(table.name);
95
    }
96
97
    toJSON(): object {
98
        const object = {};
99
        object[this.$name] = this.value;
100
        return object;
101
    }
102
103
    protected setParentProperties(): Field {
104
        return this;
105
    }
106
}